package com.lrh.java8;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AppleFilter {

    @FunctionalInterface
    public interface FilterApple{
        boolean filter(Apple apple);
    }
    public class GreenAnd150WeigthFilter implements FilterApple {
        @Override
        public boolean filter(Apple apple) {
            return (apple.getColor().equals("green") && apple.getWeigth() >= 160);
        }
    }

    public static List<Apple> findApple(List<Apple> apples, FilterApple filterApple){
        List<Apple> list = new ArrayList<>();
        for(Apple apple : apples){
            if(filterApple.filter(apple)){
                list.add(apple);
            }
        }
        return list;
    }

    public static List<Apple> findGreenApple(List<Apple> apples){
        List<Apple> list = new ArrayList<>();
        for(Apple apple : apples){
            if("green".equals(apple.getColor())){
                list.add(apple);
            }
        }
        return list;
    }

    public static List<Apple> findGreenApple(List<Apple> apples, String color){
        List<Apple> list = new ArrayList<>();
        for(Apple apple : apples){
            if(color.equals(apple.getColor())){
                list.add(apple);
            }
        }
        return list;
    }
    public static void main(String[] args) {
        List<Apple> list = Arrays.asList(new Apple("green", 150), new Apple("yellow", 130), new Apple("red", 124), new Apple("green", 220));
        List<Apple> greenApples = findGreenApple(list);
        System.out.println(greenApples);
        System.out.println("===============1==============");
        List<Apple> greenApples2 = findGreenApple(list, "green");
        System.out.println(greenApples2);
        System.out.println("===============2==============");
        List<Apple> greenApples3 = findGreenApple(list, "red");
        System.out.println(greenApples3);
        System.out.println("===============3==============");
        List<Apple> greenApples4 = findApple(list, new AppleFilter().new GreenAnd150WeigthFilter());
        System.out.println(greenApples4);
        System.out.println("===============4==============");

        //java8
        //lambda表达式：FilterApple只能有一个接口方法（default方法除外）
       /* List<Apple> lambdaResult = findApple(list, (Apple apple) -> {
            return (apple.getColor().equals("green") && apple.getWeigth() >= 160);
        });*/
        List<Apple> lambdaResult = findApple(list, apple -> {
            return (apple.getColor().equals("green") && apple.getWeigth() >= 160);
        });
        System.out.println(lambdaResult);

        //线程
        Thread thread = new Thread(new Runnable(){
            @Override
            public void run() {

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName());
            }
        });
        thread.start();
        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("2111111");


        System.out.println("===============5==============");


        //java 8
        new Thread( () ->System.out.println(Thread.currentThread().getName())).start();

    }
}
